package com.csgsystems.im.survey.interceptor;

import java.text.SimpleDateFormat;
import java.util.TimeZone;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.StrutsStatics;

import com.csgsystems.im.Constants;
import com.csgsystems.im.hierarchy.service.HierarchyService;
import com.csgsystems.im.identity.model.Login;
import com.csgsystems.im.struts2.interceptor.InterceptorException;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class SingleSignOnMockInterceptor implements Interceptor {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1232230683735867890L;
	private static Logger log = Logger.getLogger(SingleSignOnMockInterceptor.class);
	public static SimpleDateFormat timestampFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
	
	private HierarchyService hierarchyService = null;
	
	@Override
	public void destroy() {

	}

	@Override
	public void init() {

	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		
		final ActionContext context = invocation.getInvocationContext();
		HttpServletRequest request = (HttpServletRequest) context.get(StrutsStatics.HTTP_REQUEST);
		HttpSession session = request.getSession();

		/*
		 * If the Login object has not been constructed yet, create a mock object.
		 */
		if (session.getAttribute(Constants.SSO_CONTEXT) == null) {

			Login login = new Login();
			login.setHierarchyId(1);
			login.setCompanyId(1);
			login.setFirstName("Mock");
			login.setLastName("User");
			login.setAccountStatus(Login.STATUS_ACTIVE);
			login.setPhoneNumber("4025551212");
			login.setEmailAddress("abuse@csgsystems.com");

			try {
				session.setAttribute(Constants.SSO_CONTEXT, populateHiearchy(login));
				TimeZone timeZone = TimeZone.getTimeZone("US/Central");
				session.setAttribute(Constants.TIMEZONE_CONTEXT, timeZone);

			} catch (Exception e) {
				log.error("Could not set MOCK login on session.", e);
				return "objectError";
				
			}
				
			
		} 

		return invocation.invoke();
		
	}
	
	/**
	 * Load Company and Hierarchy objets.  
	 * 
	 * 
	 * @param login
	 * @return login with Company and Hierarchy populated.
	 */
	private Login populateHiearchy(Login login) {
		
		try {
			login.setCompany(hierarchyService.findCompanyById(login.getCompanyId()));
			
		} catch (Exception e) {
			throw new InterceptorException("Unable to set Company during SSO process.", e);
		}
		
		try {
			login.setHierarchyItem(hierarchyService.findHierarchyItemById(login.getHierarchyId()));
			
		} catch (Exception e) {
			throw new InterceptorException("Unable to set Hierarchy during SSO process.", e);
			
		}
		return login;
		
	}

	public void setHierarchyService(HierarchyService hierarchyService) {
		this.hierarchyService = hierarchyService;
	}

}
